home *** CD-ROM | disk | FTP | other *** search
/ Linux Cubed Series 3: Developer Tools / Linux Cubed Series 3 - Developer Tools.iso / utils / file / managers / git-4.3 / git-4 / git-4.3.7 / src / xmalloc.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-07-08  |  2.1 KB  |  94 lines

  1. /* xmalloc.c -- safe memory management routines. Includes xmalloc, xcalloc
  2.    and xrealloc. fatal() is called when there is no more memory available. */
  3.  
  4. /* Copyright (C) 1993, 1994, 1995 Free Software Foundation, Inc.
  5.  
  6.    This program is free software; you can redistribute it and/or modify
  7.    it under the terms of the GNU General Public License as published by
  8.    the Free Software Foundation; either version 2, or (at your option)
  9.    any later version.
  10.  
  11.    This program is distributed in the hope that it will be useful,
  12.    but WITHOUT ANY WARRANTY; without even the implied warranty of
  13.    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14.    GNU General Public License for more details.
  15.  
  16.    You should have received a copy of the GNU General Public License
  17.    along with this program; if not, write to the Free Software
  18.    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.  */
  19.  
  20. /* Written by Tudor Hulubei and Andrei Pitis.  */
  21.  
  22.  
  23. #ifdef HAVE_CONFIG_H
  24. #include <config.h>
  25. #endif
  26.  
  27. #include "xmalloc.h"
  28.  
  29.  
  30. void fatal __P((char *));
  31.  
  32.  
  33. char *
  34. xmalloc(size)
  35.     size_t size;
  36. {
  37.     void *pointer = malloc(size ? size : 1);
  38.  
  39.     if (pointer == NULL)
  40.         fatal("xmalloc: virtual memory exhausted");
  41.  
  42.     return (char *)pointer;
  43. }
  44.  
  45.  
  46. int xcalloc_call_count = 0;
  47.  
  48. char *
  49. xcalloc(count, itemsize)
  50.     size_t count, itemsize;
  51. {
  52.     void *pointer;
  53.  
  54.     xcalloc_call_count++;
  55.  
  56.     if (count && itemsize)
  57.         pointer = calloc(count, itemsize);
  58.     else
  59.         pointer = calloc(1, 1);
  60.  
  61.     if (pointer == NULL)
  62.         fatal("xcalloc: virtual memory exhausted");
  63.  
  64.     return (char *)pointer;
  65. }
  66.  
  67.  
  68. char *
  69. xrealloc(pointer, size)
  70.     void *pointer;
  71.     size_t size;
  72. {
  73.     /* I know realloc should call malloc if 'pointer' is NULL, but it seems
  74.        to work better on suns. Strange... */
  75.     void *new_pointer = pointer ? realloc(pointer, size ? size : 1) :
  76.                                   malloc(size ? size : 1);
  77.  
  78.     if (new_pointer == NULL)
  79.         fatal("xrealloc: virtual memory exhausted");
  80.     
  81.     return (char *)new_pointer;
  82. }
  83.  
  84.  
  85. void
  86. xfree(pointer)
  87.     void *pointer;
  88. {
  89.     if (pointer)
  90.         free(pointer);
  91.     else
  92.         fatal("xfree: trying to free NULL pointer");
  93. }
  94.